package mytest.wb4;

import cn.hutool.core.map.MapUtil;
import cn.hutool.log.StaticLog;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import pojo.AutoImportData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
public class PushDianXiaoMain {
    public static void main(String[] args) {
        AutoImportData autoImportData = new AutoImportData();
        List<String> userIds = new ArrayList<>();
//        userIds.add("a81cbb89-3440-4347-81df-d225ac5cd2e3");


        autoImportData.setCusIds(userIds);
        autoImportData.setCreateTime(System.currentTimeMillis());
        // 测试环境获额未交易
//        autoImportData.setRuleId("20201030181722322560604481667072");

//        //测试环境还款提醒
//        autoImportData.setRuleId("20240816041047824722833865375744");

        autoImportData.setFlag(true);
        autoImportData.setRuleId("20201119172910329796231262507008");
        input.TaskManualImportDto dto3 = new input.TaskManualImportDto("7021122554", null, null, "0", "3", null, null);
        List<input.TaskManualImportDto> importList = new ArrayList<>();
        importList.add(new input.TaskManualImportDto("7021122554", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:oZbH4YELyXzPSxcaspZ1bcB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:vqpciUQjcfo6KACvnWe/acB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:7V/08MQH0TUVChnFovL6a8B9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:QOWl7kKv97t5NxSCvFuADMB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:d9tKU7LClC5UnR7YAzRi2MB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:OSIXgrxpLeLdX0aGYa/f8sB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:2E9Rk2Wq6KfLLcRwOwqCUsB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:NeDaVu0wbTilodfjmi0s8sB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
//        importList.add( new input.TaskManualImportDto("2:UffD/55CbYNKjMbo5vOXEsB9J8thdk3D0Y+KhnpL+to=", null, null, "0", "3", null, null));
        autoImportData.setImportList(importList);
        sendDX(autoImportData, null);
    }

    //
    private static void sendDX(AutoImportData autoImportData, List<Map<String, String>> insertList) {
//        AuthTokenHelper authTokenHelper = new AuthTokenHelper("zzfca6700d28f849bbb0e0090a5caecba5", "sb4517eac3d924e4590268bbca465ea30");
        // send and insert or update
        String smsPushUrl = "https://test-leading.alend.ng/flute/api/v1/autoImportData";
//        String smsPushUrl = "https://leading.alend.ng/flute/api/v1/autoImportData";
//        String smsPushUrl = "https://leading.aidacredit.ng/flute/api/v1/autoImportData";
//        String smsPushUrl = "http://api-proxy:1080/https/mfbleading/flute/api/v1/autoImportData";
//        String smsPushUrl = "";
        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(smsPushUrl);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        String urlPath = "/flute/api/v1/autoImportData";
        String method = "POST";


        String reqParams = new Gson().toJson(autoImportData);
        StaticLog.info("SaleOrder | sendDX | reqParams={}", reqParams);
        StringEntity stringEntity = null;
        try {
            stringEntity = new StringEntity(reqParams);
            stringEntity.setContentType("text/json");
            httpPost.setEntity(stringEntity);

            String queryParam = null;
//            String token = authTokenHelper.generateToken(urlPath, method, queryParam, reqParams, (int)(System.currentTimeMillis()/1000));
//            httpPost.setHeader("X-NewAuth-Token", token);
//            httpPost.setHeader("X-WeshareAuth-Token", token);

            // 发送请求
            CloseableHttpResponse response = build.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");
//             response={"retCode":"0000","retMsg":"success"}
            StaticLog.info("SaleOrder | response={}", result);
//            String result = "{\"status\":0,\"message\":\"Push succeeded\",\"content\":null}";
            Map map = new Gson().fromJson(result, Map.class);
            Integer status = MapUtil.getInt(map, "status", -1);
//            response={"status":0,"message":"Push succeeded","content":null}

//            if (status == 0 && CollectionUtil.isNotEmpty(insertList)) {
//                dataInsert(insertList);
//            }
        } catch (Exception e) {
            log.error("", e);
        }
    }


    private static List<String> getUserIds() {
        List<String> userIds = new ArrayList<>();


        return userIds;
    }


}
